home *** CD-ROM | disk | FTP | other *** search
- // Chap19_2.cpp
- // This is not a complete program. Notice, however, the use
- // of dynamic_cast.
- class Account
- {
- public:
- Account(int accNo, float initialBalance);
-
- virtual void deposit(float amount);
- virtual void withdrawal(float amount);
-
- // addAccount - add current account to the account list
- void addAccount();
-
- // getNext - get the next account from the account list;
- // if pPrev == 0, return first;
- // return 0 when no more left in list
- static Account* getNext(Account *pPrev);
- };
-
- class Checking : public Account
- {
- public:
- Checking(int accNo, float initialBalance = 0.0F);
- };
-
- class Savings : public Account
- {
- public:
- Savings(int accNo, float initialBalance = 0.0F);
-
- // addInterest - calculate the interest for one time
- // period and add it back in
- void addInterest();
- };
-
- // accInterest - loop thru the accounts. For each Savings
- // account you find, calculate the interest and
- // it back in
- void accInterest()
- {
- // loop thru the Accounts
- Account* pA = (Account*)0;
- while(pA = Account::getNext(pA))
- {
- // only process Savings accounts
- // make a type-safe downcast:
- Savings* pS = dynamic_cast<Savings*>(pA);
-
- // if pS is non-zero then this really was a Savings
- if (pS)
- {
- // so add the interest in
- pS->addInterest();
- }
- }
- }
-